package com.example.wechatscancode.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptUtils {
    public static String getSHA(String info) {
    byte[] digesta = null;
        try {
// 得到一个SHA-1的消息摘要
        MessageDigest alga = MessageDigest.getInstance("SHA-1");
// 添加要进行计算摘要的信息
        alga.update(info.getBytes());
// 得到该摘要
        digesta = alga.digest();
    } catch (
    NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    // 将摘要转为字符串
    String rs = byte2hex(digesta);
        return rs;
}

    private static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (byte aB : b) {
            stmp = (Integer.toHexString(aB & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs;
    }
}
